/*
* Copyright 2015 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import Foundation

extension Dictionary where Value: Equatable {
  // MARK: - Public

  /**
  Returns a list of keys in the dictionary that match the given value.

  - parameter value: The value to find.
  - returns: The list of keys for that value.
  */
  public func bky_allKeysForValue(_ value: Value) -> [Key] {
    return self.filter { $1 == value }.map { $0.0 }
  }

  /**
  Returns any key in the dictionary that matches the given value. There is no guarantee on what
  key this may be, if there are multiple keys that contain the same value.

  - parameter value: The value to find.
  - returns: Any key mapped for the given value, or `nil` if none is found.
  */
  public func bky_anyKeyForValue(_ value: Value) -> Key? {
    let possibleKeys = bky_allKeysForValue(value)
    return !possibleKeys.isEmpty ? possibleKeys[0] : nil
  }
}
